#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "lm4f120h5qr.h"

#define POT_LEFT  1
#define POT_RIGHT 0

extern volatile bool mSecondAlert;
extern void EnableInterrupts(void);
extern void DisableInterrupts(void);
extern void GetADCval(uint32_t Channel);

// Push Buttons
extern volatile uint16_t SW2count;
extern volatile uint16_t SW3count;
extern volatile uint16_t SW4count;
extern volatile uint16_t SW5count;
extern volatile uint16_t SW6count;

extern volatile bool SW2button;
extern volatile bool SW3button;
extern volatile bool SW4button;
extern volatile bool SW5button;
extern volatile bool SW6button;

// ADC
extern volatile uint32_t POT300Value;
extern volatile uint32_t POT301Value;

 /****************************************************************************
 * The SysTick Handler
 ****************************************************************************/

// This is place holder to shift the GPIO data pin to bit 0
// This should be replaced as soon as I determine a more elegant way to do this


void SYSTICKIntHandler(void)
{
	uint16_t GPIOpin = 0x00;

	// Disable Interrupts
	DisableInterrupts();

	// Set ms flag
	mSecondAlert = true;

	// Test SW2
	GPIOpin = GPIO_PORTA_DATA_R & (1 << 6);	// Remove extra pin data
	GPIOpin = (GPIOpin >> 6);	// Shift pin data to bit 0
	SW2count = (SW2count << 1);	// Shift left by 1
	SW2count |= GPIOpin;
	if (SW2count == 0x8000) {
		SW2button = true;
	}
	// Test SW3
	GPIOpin = GPIO_PORTA_DATA_R & (1 << 7);
	GPIOpin = (GPIOpin >> 7);
	SW3count = (SW3count << 1);	// Shift left by 1
	SW3count |= GPIOpin;
	if (SW3count == 0x8000) {
		SW3button = true;
	}
	// Test SW4
	GPIOpin = GPIO_PORTD_DATA_R & (1 << 2);
	GPIOpin = (GPIOpin >> 2);
	SW4count = (SW4count << 1);	// Shift left by 1
	SW4count |= GPIOpin;
	if (SW4count == 0x8000) {
		SW4button = true;
	}
	// Test SW5
	GPIOpin = GPIO_PORTD_DATA_R & (1 << 3);
	GPIOpin = (GPIOpin >> 3);
	SW5count = (SW5count << 1);	// Shift left by 1
	SW5count |= GPIOpin;
	if (SW5count == 0x8000) {
		SW5button = true;
	}
	// Test SW6
	GPIOpin = GPIO_PORTD_DATA_R & (1 << 1);
	GPIOpin = (GPIOpin >> 1);
	SW6count = (SW6count << 1);	// Shift left by 1
	SW6count |= GPIOpin;
	if (SW6count == 0x8000) {
		SW6button = true;
	}
	// GET ADC
	GetADCval(POT_RIGHT);
	GetADCval(POT_LEFT);

	// Clear the SysTick Interrupt
	NVIC_ST_CURRENT_R = 0;	// clear the current count
	// Enable Interrupts
	EnableInterrupts();
}

/****************************************************************************
 * Initialize the SysTick timer to a given count.
 * Turns on Interrupts
 ****************************************************************************/
void initializeSysTick(uint32_t count)
{
	uint32_t savedVal = NVIC_ST_CTRL_R;
	NVIC_ST_CTRL_R = 0;	// disable SysTick timer
	NVIC_ST_RELOAD_R = count - 1;	// Set reload to 1ms
	NVIC_ST_CURRENT_R = 0;	// clear the current count

	// Enable Interrupts
	NVIC_ST_CTRL_R |=
	    (NVIC_ST_CTRL_ENABLE | NVIC_ST_CTRL_CLK_SRC |
	     NVIC_ST_CTRL_INTEN);
}
